package com.ftd.controller;

import com.ftd.DBConnection.DBConnection;
import com.ftd.exception.ApiException;
import com.ftd.model.Ebook;
import com.ftd.model.User;
import com.ftd.util.DigestUtil;
import com.ftd.viewmodel.ApiResult;
import com.ftd.viewmodel.MapResult;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;

/**
 * Created by apple on 16/8/3.
 */
@RestController
public class RegisterController extends BaseController {
    @RequestMapping("saveInBackground")
    public ApiResult saveInBackground(String phonenumber){
        System.out.println("houtaiyunxing");

        ArrayList list = (ArrayList) DBConnection.select4("user");
        MapResult mapResult = new MapResult();

        for (int i = 0; i < list.size();i++) {
            User user = (User)list.get(i);
            if (user.getPhonenumber().equals(phonenumber)){
                mapResult.put("phonenumber", user.getPhonenumber());
                return mapResult;
            }
        }
        DBConnection.insert10(phonenumber);

        DBConnection.insert(phonenumber);


        mapResult.put("token", DigestUtil.Encrypt("0:" + phonenumber));

        return mapResult;


    }
//    @RequestMapping("saveInBackground/{phonenumber}")
//    @ResponseBody
//    public boolean saveInBackground(@PathVariable String phonenumber){
//        System.out.println(phonenumber);
//        DBConnection.insert(phonenumber);
//        return true;
//    }


}
